import entity.Employee;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class UseStream {

    private static List<Employee> LIST = new ArrayList<>();
    static {
        LIST.add(new Employee("杰哥",30,"一个光头","江苏"));
        LIST.add(new Employee("阿伟",20,"一个少年","江苏"));
        LIST.add(new Employee("小黄",24,"写Java的","浙江"));
        LIST.add(new Employee("小红红",24,"写Python的","浙江"));
        LIST.add(new Employee("小黑",28,"写Bug的","上海"));
        LIST.add(new Employee("小绿人",26,"写React的","上海"));
    }

    public static void main(String[] args) {
//        common();
//        useStream();

        finaAverageAgeA();
        System.out.println("------------------分割线------------------");
        finaAverageAgeB();
        System.out.println("------------------分割线------------------");
        finaAverageAgeStream();
        System.out.println("------------------分割线------------------");
        useStreamAndLambda();

    }

    private static void common(){
        //常规方法
        List<Employee> list = new ArrayList<>();
        for (Employee e:LIST) {
            if(e.getAge()>25){
                list.add(e);
            }
        }
        System.out.println(list);
    }

    private static void useStream(){
        //使用stream
        List<Employee> list = LIST.stream()
                .filter(x->x.getAge()>25)
                .collect(Collectors.toList());
        System.out.println(list);
    }

    private static void useStreamAndLambda(){
        Predicate<Employee> ageFilter = (x)->x.getAge()>25;
        Predicate<Employee> nameFilter = (x)->x.getName().length()>2;
        //使用stream
        List<Employee> list = LIST.stream()
                .filter(ageFilter.and(nameFilter))
                .collect(Collectors.toList());
        System.out.println(list);
    }



    //求出各个出生地的员工的平均年龄
    private static void finaAverageAgeA(){
        //分组
        Map<String,List<Employee>> maps = new HashMap<>();
        for (Employee e:LIST){
            maps.put(e.getBirthPlace(),new ArrayList<>());
        }
        maps.keySet().forEach(value->{
            List<Employee> list = new ArrayList<>();
            for (Employee e:LIST) {
                if (e.getBirthPlace().equals(value)) {
                    list.add(e);
                }
            }
            maps.put(value,list);
        });
        //求平均数
        maps.forEach((key, value) -> {
            int totalAge = 0;
            for (Employee e : value) {
                totalAge += e.getAge();
            }
            System.out.println(key+"--"+totalAge / value.size());
        });

    }

    //求出各个出生地的员工的平均年龄
    private static void finaAverageAgeB(){
        //使用stream
        Map<String,List<Employee>> maps = new HashMap<>();
        for (Employee e:LIST){
            //computeIfAbsent如果不存在就创建一个key-value
            List<Employee> list = maps.computeIfAbsent(e.getBirthPlace(),
                    x -> new ArrayList<>());
            list.add(e);
        }
        maps.forEach((key, value) -> {
            int totalAge = 0;
            for (Employee e : value) {
                totalAge += e.getAge();
            }
            System.out.println("B--"+key+"--"+totalAge / value.size());
        });

    }


    //求出各个出生地的员工的平均年龄
    private static void finaAverageAgeStream(){
        //分组
        Map<String, Double> collect = LIST.stream()
                .collect(Collectors.groupingBy(Employee::getBirthPlace,
                        Collectors.averagingInt(Employee::getAge)));
        System.out.println(collect);


    }



}
